﻿using System.Collections.Generic;
using UnityEngine;

public class GameObjectPool : ObjectPool<GameObject>
{
	public List<GameObject> Pool = new List<GameObject>();

	public GameObject GetObject()
	{
		GameObject go = null;
		lock (Pool)
		{
			if (Pool.Count > 0)
			{
				go = Pool[0];
				go.SetActive(true);
			}

			Remove(go);
		}

		return go;
	}

	public void ReturnObject(GameObject go)
	{
		go.SetActive(false);
		Add(go);
	}


	public void Add(GameObject ctrl)
	{
		if (!Pool.Contains(ctrl))
		{
			Pool.Add(ctrl);
		}
	}

	public void Remove(GameObject ctrl)
	{
		if (Pool.Contains(ctrl))
		{
			Pool.Remove(ctrl);
		}
	}

	public void Clear()
	{
		Pool.Clear();
	}
}


public interface ObjectPool<T> where T : Object
{
	T GetObject();
	void ReturnObject(T t);
	void Add(T t);
	void Remove(T t);
	void Clear();
}
